/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.wisc.csvParser.plugins;

import com.wisc.csvParser.plugins.DataFilterStreamMonitor.events;


/**
 *
 * @author  user
 */
public class JPanelStreamMonitor extends javax.swing.JPanel 
        implements DataFilterStreamMonitor.IEventListener{
    
    private DataFilterStreamMonitor monitor;
    
    /** Creates new form JPanelStreamMonitor
     * @param monitor Underlying stream monitor object.
     */
    public JPanelStreamMonitor(DataFilterStreamMonitor monitor) {
        initComponents();
        
        this.monitor = monitor;
        monitor.addEventListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        monitoredSitesList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        stopMonitoringButton = new javax.swing.JButton();
        startedCB = new javax.swing.JCheckBox();

        jScrollPane1.setViewportView(monitoredSitesList);

        jLabel1.setText("Sites Seen:");

        stopMonitoringButton.setText("Stop Monitoring");
        stopMonitoringButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopMonitoringButtonActionPerformed(evt);
            }
        });

        startedCB.setText("Started?");
        startedCB.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
                        .addComponent(startedCB))
                    .addComponent(stopMonitoringButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(startedCB)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(stopMonitoringButton)))
                .addContainerGap(110, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void stopMonitoringButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopMonitoringButtonActionPerformed
        if(monitoredSitesList.getSelectedValue()!= null &&
                monitoredSitesList.getSelectedValue() != ""){
            monitor.removeSite((String)monitoredSitesList.getSelectedValue());
            
        }
}//GEN-LAST:event_stopMonitoringButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList monitoredSitesList;
    private javax.swing.JCheckBox startedCB;
    private javax.swing.JButton stopMonitoringButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void raiseEvent(events e) {
        if(e==events.newDataSeen){
            monitoredSitesList.setListData(monitor.getSites());
            
        }else if(e==events.sitesChanged){
            monitoredSitesList.setListData(monitor.getSites());
        }else if(e==events.started || e==events.stopped){
            setStartStopState(monitor.isStarted());
        }
    }
    
    private void setStartStopState(boolean started){
        startedCB.setSelected(started);
    }

    
}
